<template>
	<view></view>
</template>

<script>
// let interstitialAd = null;
// 抖音配置
let ttConfig = require("../../../util/ttConfigSet.js");
export default {
	data() {
		return {
			ttConfigMess: {},
			interstitialAd: null
		};
	},
	onReady() {
		let ttAccountInfo = tt.getEnvInfoSync() && tt.getEnvInfoSync().microapp
		this.ttConfigMess = ttConfig[ttAccountInfo.appId] || {}
		this.handleAdView()
	},

	methods: {
		isShowAd() {
			console.log('--------------------------------------------------------');
			if (this.ttConfigMess && this.ttConfigMess.insertAdLockId) {
				console.log('插屏上报');
				this.$statisAd({
					action: 'REQUEST',
					adId: this.ttConfigMess.insertAdLockId,
					adSlot: 4
				});
				console.log('ad----', this.interstitialAd)
				this.interstitialAd.load()
				.then(() => {
					const currentTimestamp = Math.floor(Date.now() / 1000);
					let timeNum = 1
					let timer = setInterval(() => {
						console.log(timeNum)
						timeNum++ 
						if(timeNum > 2) {
							this.interstitialAd.show().then(() => {
								// this.interstitialAd && this.interstitialAd.destroy()
							}).catch(err => {
								setTimeout(() => {
									this.interstitialAd.show()
								}, 3)
								console.log(err, 'err----------------')
							})
							clearInterval(timer)
						}
					}, 1000)
					this.$statisAd({
						action: 'EXPOSURE',
						adId: this.ttConfigMess.insertAdLockId,
						adSlot: 4,
					});
					getApp().globalData.lastAdTimestamp = currentTimestamp;
					console.log('拉起');
				})
				.catch(err => {
					uni.hideLoading()
					this.interstitialAd = null;
					console.log('拉起失败');
				})
				// this.interstitialAd.show().then(() => {
				// 	this.$statisAd({
				// 		action: 'EXPOSURE',
				// 		adId: this.ttConfigMess.insertAdLockId,
				// 		adSlot: 4,
				// 		// episodicDramaId: String(nowItem.id),
				// 		// videoId: String(nowItem.videoId),
				// 		// authorId: this.compInfo.laikanAccountId ? String(this.compInfo.laikanAccountId) : '0',
				// 		// fakeId: String(nowItem.compilationsFakeId),
				// 	});
				// 	console.log('拉起成功');
				// })
				// 	.catch(() => {
				// 		this.interstitialAd
				// 			.load()
				// 			.then(() => {
				// 				this.interstitialAd.show()
				// 				this.$statisAd({
				// 					action: 'EXPOSURE',
				// 					adId: this.ttConfigMess.insertAdLockId,
				// 					adSlot: 4,
				// 					// episodicDramaId: String(nowItem.id),
				// 					// videoId: String(nowItem.videoId),
				// 					// authorId: this.compInfo.laikanAccountId ? String(this.compInfo.laikanAccountId) : '0',
				// 					// fakeId: String(nowItem.compilationsFakeId),
				// 				});
				// 				console.log('拉起重试');
				// 			})
				// 			.catch(err => {
				// 				uni.hideLoading()
				// 			})
				// 		console.log('拉起失败');
				// 	})
			} else {
				uni.hideLoading()
			}
		},

		handleAdView() {
			// 获取配置信息
			if (this.ttConfigMess && this.ttConfigMess.insertAdLockId) {
				// 创建实例
				this.interstitialAd = tt.createInterstitialAd({
					adUnitId: this.ttConfigMess.insertAdLockId || "nvlnc9w9g8yn967sal",
				})
				console.log('创建实例---------------');
				this.interstitialAd.onError((err) => {
					uni.hideLoading()
				})

				this.interstitialAd.onClose(() => {
					console.log('closeAd')
					this.interstitialAd.destroy()
				})
				// this.$emit('adSuccess')
				// ({ isEnded }) => {
				// 	uni.hideLoading()
				// 	console.log('关闭', isEnded)
				// 	if(isEnded) { // 观看完毕解锁

				// 	} else { // 未观看完毕
				// 	}
				// }
				// 预加载资源
				// this.interstitialAd.load().then(() => {
				// 	console.log("广告加载成功");
				// 	this.$emit('adSuccess')
				// })

			}
		}
	},
	onUnload() {
		this.interstitialAd.destroy();
		this.interstitialAd = null;
	}
}
</script>
